﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsfoodorderfeedServices : Repository<corecmsfoodorderfeed>, IcorecmsfoodorderfeedServices
    {
        public bool InsertAsync(corecmsfoodorderfeed entity)
        {
            return base.Change<corecmsfoodorderfeed>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsfoodorderfeed> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsfoodorderfeed entity)
        {
            return base.Change<corecmsfoodorderfeed>().Update(a => new corecmsfoodorderfeed()
            {
                detailId = entity.detailId,
                orderId = entity.orderId,
                feedId = entity.feedId,
                quantity = entity.quantity,
                price = entity.price
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsfoodorderfeed> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsfoodorderfeed>().Update(a => new corecmsfoodorderfeed()
                {
                    detailId = item.detailId,
                    orderId = item.orderId,
                    feedId = item.feedId,
                    quantity = item.quantity,
                    price = item.price
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfoodorderfeed>().DeleteById(id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsfoodorderfeed>().DeleteById(id);
            }
            return returnKey;
        }

        public List<corecmsfoodorderfeed> SelectByOrderId(List<long> orderIds)
        {
            List<corecmsfoodorderfeed> lst = base.AsQueryable().Where(p => orderIds.Contains(p.orderId)).ToList();
            return lst;
        }

        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodorderfeed> GetCaChe()
        {
            return base.GetList();
        }

        #endregion


        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodorderfeed> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsfoodorderfeed> custos = new List<corecmsfoodorderfeed>();
            var where = Expressionable.Create<corecmsfoodorderfeed>();
            //where = where.And(a => a.isDelete == false);
            //if (!string.IsNullOrEmpty(sName))
            //{
            //    where = where.And(a => a.name.Contains(sName))
            //        .Or(a => a.number.Contains(sName)).Or(a => a.name.Contains(sName));
            //}
            //if (pageIndex >= 1 && pageSize > 0)
            //{
            //    custos = base.AsQueryable().WhereIF(true, where.ToExpression())
            //        .ToPageList((int)pageIndex, (int)pageSize, ref count)
            //        .OrderBy(a => a.number).ToList();
            //}
            //else
            //{
            custos = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            //}
            return custos;
        }
        #endregion
    }
}
